//
//  FLNSubTableViewController.m
//  百思不得姐
//
//  Created by xmg on 16/6/4.
//  Copyright © 2016年 FLN.com. All rights reserved.
//

#import "FLNSubTableViewController.h"
#import "UIColor+Hex.h"
#import "FLNSubTableViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "FLNSubTagItem.h"
#import <SVProgressHUD/SVProgressHUD.h>
static NSString * const ID = @"fuck";

@interface FLNSubTableViewController ()
@property (nonatomic, strong) NSArray *tags;
@property (nonatomic, weak) AFHTTPSessionManager *mgr;
@end

@implementation FLNSubTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.title = @"推荐标签";
    
    
    //注册的cell,xib可以不绑定标识
    [self.tableView registerNib:[UINib nibWithNibName:@"FLNSubTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    
    //显示指示器
    [SVProgressHUD showWithStatus:@"等下有美女出来......"];
    
    //加载真实数据 -> 查看接口文档(1.基本url 2.请求参数 3.请求方式)
    [self loadData];
    
    self.tableView.backgroundColor = [UIColor purpleColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    
    //隐藏显示器
    [SVProgressHUD dismiss];
    
    //取消请求
    [_mgr.tasks makeObjectsPerformSelector:@selector(cancel)];

}

- (void)loadData {
    //1.创建请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    //2.拼接请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    //3.发送请求
    
    [mgr GET:FLNBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //隐藏指示器
        [SVProgressHUD dismiss];
        
        [responseObject writeToFile:@"/Users/xmg/Desktop/baisi/baisibudejie/百思不得姐/百思不得姐/tag.plist" atomically:YES];
        
        //字典转模型
        _tags = [FLNSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return _tags.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    static NSString *ID = @"cell";
    FLNSubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
    
    cell.item = _tags[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 62;
}

@end
